/**
 * 作者:hanbo
 * 日期:2019/1/2817:59
 * 描述:
 */
const map = L.map('map', {
    attributionControl: false,
    zoomControl: true,
    measureControl: true,
    maxZoom: 17,
    minZoom: 0
});
map.on("baselayerchange",function (event) {
    let mapInfo={};
    if(event.layer._url){
        mapInfo.url=event.layer._url;//注记和地图合并的图层
    }else {
        mapInfo.url=event.layer._layers[Object.keys(event.layer._layers)[0]]._url;//注记和地图分开的图层
    }
    if(event.layer.key){
        mapInfo.key=event.layer.key
}
    if(event.layer.subdomains){
        mapInfo.subdomains=event.layer.subdomains
    }
    setCurrentUrl(mapInfo)
});
map.fitWorld();
//比例尺
L.control.scale().addTo(map);

queryBaseLayer();

function initBaseLayer(array) {
    let baseLayers = {};
    for (let i = 0; i < array.length; i++) {
        let layer;
        let info = array[i];
        let option={errorTileUrl:"暂无该等级切片"};
        if(info.subdomains){
           option.subdomains=info.subdomains;
        }
        if(info.key){
            option.key=info.key;
        }
        if(info.annotion){
           layer= L.layerGroup([ L.tileLayer(info.url,option),  L.tileLayer(info.annotion,option)])
        }else {
            layer = L.tileLayer(info.url,option);
        }
        if(info.subdomains){
            layer.subdomains=info.subdomains;
        }
        if(info.key){
            layer.key=info.key;
        }

        baseLayers[info.name] = layer;
        if (info.checked) {
            let mapInfo={url:info.url};
            if(info.subdomains){
                mapInfo.subdomains=info.subdomains;
            }
            if(info.key){
                mapInfo.key=info.key;
            }
            layer.addTo(map);
            setCurrentUrl(mapInfo);
        }
    }
    let overlays = {};
    L.control.layers(baseLayers, overlays).addTo(map);
}

/**
 *请求图层信息
 */
function queryBaseLayer() {

    $.ajax({
        url: "data/mapSource.json", dataType: "json", success: function (e) {
            initBaseLayer(e);
        }
    });
}

